package com.tvunetworks.center.device.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tvunetworks.center.device.mapper.DeviceGeoFavoriteMapper;
import com.tvunetworks.center.device.mapper.DeviceGeoRecordFavoriteMapper;
import com.tvunetworks.center.device.mapper.DeviceGeoRecordMapper;
import com.tvunetworks.center.device.model.DeviceGeoRecord;
import com.tvunetworks.center.device.model.DeviceGeoRecordFavorite;
import com.tvunetworks.center.device.model.Favorite;
import com.tvunetworks.center.device.model.param.DeviceGeoRecordParam;
import com.tvunetworks.center.device.model.param.FavoriteParam;
import com.tvunetworks.center.device.service.DeviceGeoRecordService;
import com.tvunetworks.center.device.util.DistributedGlobalIdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
/**
 * @program: tvucc-instance
 * @author lebronchen
 * @create: 2019-02-28 16:53
 **/
@Slf4j
@Service
public class DeviceGeoRecordServiceImpl implements DeviceGeoRecordService {
    @Autowired
    private DeviceGeoRecordMapper deviceGeoRecordMapper;
    @Autowired
    private DistributedGlobalIdUtil distributedGlobalIdUtil;
    @Autowired
    private DeviceGeoFavoriteMapper deviceGeoFavoriteMapper;
    @Autowired
    private DeviceGeoRecordFavoriteMapper deviceGeoRecordFavoriteMapper;


    @Override
    public List<DeviceGeoRecord> list(DeviceGeoRecordParam deviceGeoRecordParam) {
        return deviceGeoRecordMapper.list(deviceGeoRecordParam);
    }

    @Override
    public void save(DeviceGeoRecord deviceGeoRecord) {
        deviceGeoRecord.setId(distributedGlobalIdUtil.netxId()+"");
        deviceGeoRecord.setCreateTime(System.currentTimeMillis());
        deviceGeoRecord.setDeleteFlag(0);
        DeviceGeoRecordParam deviceGeoRecordParam = new DeviceGeoRecordParam();
        deviceGeoRecordParam.setPeerId(deviceGeoRecord.getPeerId());
        deviceGeoRecordParam.setStartTime(deviceGeoRecord.getTimestamp()+"");
        deviceGeoRecordParam.setEndTime(deviceGeoRecord.getTimestamp()+"");
        deviceGeoRecordParam.setUserId(deviceGeoRecord.getUserId());
        List<DeviceGeoRecord> list = deviceGeoRecordMapper.list(deviceGeoRecordParam);
        if(list.size()>0){//更新
            DeviceGeoRecord deviceGeoRecordExist = list.get(0);
            deviceGeoRecord.setId(deviceGeoRecordExist.getId());
            log.error("=====Update geo record====="+JSONObject.toJSONString(deviceGeoRecord));
            deviceGeoRecordMapper.updateByPrimaryKey(deviceGeoRecord);
        }else{//新增
            deviceGeoRecordMapper.insert(deviceGeoRecord);
        }

    }

    @Override
    public void clearGeoRecordHistoryData(String endTime) {
        DeviceGeoRecordParam deviceGeoRecordParam = new DeviceGeoRecordParam();
        deviceGeoRecordParam.setEndTime(endTime);
          deviceGeoRecordMapper.clearGeoRecordHistoryData(deviceGeoRecordParam);
    }

    /**
     *  test
     */
    public void  test(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                createTestData("2000001529710462",1);
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                createTestData("ae9995f2af456c79",2);
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                createTestData("6000001152882593",3);
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                createTestData("4000001853822135",4);
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                createTestData("4b5a90de3643f7ec",4);
            }
        }).start();

    }

    @Override
    @Transactional
    public void addFavorite(FavoriteParam favoriteParam) {
        Favorite favorite =  new Favorite();
        favorite.setId(distributedGlobalIdUtil.netxId()+"");
        favorite.setCreateTime(System.currentTimeMillis()+"");
        favorite.setDeleteFlag("0");
        favorite.setName(favoriteParam.getName());
        favorite.setPeerId(favoriteParam.getPeerId());
        favorite.setStartTime(favoriteParam.getStartTime());
        favorite.setEndTime(favoriteParam.getEndTime());
        favorite.setUpdateTime(System.currentTimeMillis()+"");
        favorite.setPeerName(favoriteParam.getPeerName());
        favorite.setUserId(favoriteParam.getUserId());
        favorite.setType(favoriteParam.getType());
        deviceGeoFavoriteMapper.insert(favorite);//保存favorite信息
        //查询轨迹数据
        DeviceGeoRecordParam deviceGeoRecordParam =  new DeviceGeoRecordParam();
        deviceGeoRecordParam.setPeerId(favoriteParam.getPeerId());
        deviceGeoRecordParam.setStartTime(favoriteParam.getStartTime());
        deviceGeoRecordParam.setEndTime(favoriteParam.getEndTime());
        deviceGeoRecordParam.setUserId(favoriteParam.getUserId());

        List<DeviceGeoRecord>  deviceGeoRecordList  = deviceGeoRecordMapper.list(deviceGeoRecordParam);
        //查询收藏表的数据
        List<DeviceGeoRecord> deviceGeoRecordFavoriteList =deviceGeoRecordMapper.listFavorite(deviceGeoRecordParam);
        if(deviceGeoRecordFavoriteList.size()>0){
            deviceGeoRecordList.removeAll(deviceGeoRecordFavoriteList);
        }
        List<DeviceGeoRecordFavorite> deviceGeoRecordFavorites = null;
        if(deviceGeoRecordList.size()>0){
            deviceGeoRecordFavorites = new ArrayList<>();
            for (DeviceGeoRecord deviceGeoRecord:deviceGeoRecordList){

                DeviceGeoRecordFavorite deviceGeoRecordFavorite= new DeviceGeoRecordFavorite();
                BeanUtils.copyProperties(deviceGeoRecord,deviceGeoRecordFavorite);
                deviceGeoRecordFavorite.setId(distributedGlobalIdUtil.netxId()+"");
                deviceGeoRecordFavorites.add(deviceGeoRecordFavorite);
//                deviceGeoRecordFavoriteMapper.insert(deviceGeoRecordFavorite);
            }
            //保存数据到收藏表
            deviceGeoRecordFavoriteMapper.insertList(deviceGeoRecordFavorites);
        }



    }

    @Override
    public PageInfo<Favorite> listFavirote(FavoriteParam favoriteParam) {
        PageHelper.startPage(favoriteParam.getCurrentPage(),favoriteParam.getPageSize());
        List<Favorite> favoritesList =  deviceGeoFavoriteMapper.list(favoriteParam);
        return new PageInfo<>(favoritesList);
    }

    @Override
    public List<DeviceGeoRecordFavorite> listRecordFavorite(DeviceGeoRecordParam deviceGeoRecordParam) {
        return deviceGeoRecordFavoriteMapper.listFavorite(deviceGeoRecordParam);
    }

    @Override
    public void delFavorite(FavoriteParam favoriteParam) {
        deviceGeoFavoriteMapper.deleteById(favoriteParam.getId());
    }

    /**
     *
     * @param peerId peerId
     * @param threadId threadId
     */
    public void createTestData(String peerId,int threadId){
        Float longitude =121.4271426f;
        Float latitude = 31.1980775f;
        Integer num = 0;
        Long timestamp = 1602568900000L;
        Long startTime = System.currentTimeMillis();
        for (int i = 0; i <100000 ; i++) {
            DeviceGeoRecord deviceGeoRecord = new DeviceGeoRecord();
            deviceGeoRecord.setPeerId(peerId);
            deviceGeoRecord.setBitrate(5120F);
            longitude = longitude+0.001f;
            latitude = latitude+0.001f;
            deviceGeoRecord.setLatitude(String.valueOf(latitude));
            deviceGeoRecord.setLongitude(String.valueOf(longitude));
            deviceGeoRecord.setRealTimeParams("{\\\"type\\\":\\\"SDI\\\",\\\"format\\\":\\\"1080i59.94\\\",\\\"delay\\\":\\\"2.0 s\\\",\\\"error\\\":0,\\\"quality\\\":39.3382,\\\"slotInfo\\\":[1,1,2,2,2,2,0,0,0,3,0,0,0,3],\\\"img\\\":\\\"https://blob.tvunetworks.com/t/03056A067275D869/1603251839?type=original&app=cc&0.3070937141778529&tl=60\\\",\\\"voiceInfo\\\":{\\\"DB\\\":[-33,-33,-40,-40,-40,-40,-40,-40],\\\"RightDb\\\":-33,\\\"LeftDb\\\":-33},\\\"signalStrength\\\":\\\"\\\"}");
            timestamp++;
            deviceGeoRecord.setTimestamp(timestamp);
            deviceGeoRecord.setCreateTime(System.currentTimeMillis());
            deviceGeoRecord.setDeleteFlag(0);
            this.save(deviceGeoRecord);
            System.out.println("insert record num:"+num);
            num++;
        }
        System.out.println("ThreadId"+threadId+",Excute Time:"+(System.currentTimeMillis()-startTime));
    }
}
